package com.masterclient.app.businessmodule.homemodule.view.adapter.saletab;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eoner.commonbean.product.ProductMessage;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.eoner.baselibrary.utils.Utils;
import com.masterclient.app.modules.goods.newpage.activity.NewCommodityDetailActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class TimeLimitedProductAdapter extends RecyclerView.Adapter {


    private Context context;
    private List<ProductMessage> shProducts;
    private String eventId;
    private String status, startTime, sourceIntent;
    private OnClickStatusListener clickStatusListener;


    public TimeLimitedProductAdapter(Context context, List<ProductMessage> shProducts
            , String status, String sourceIntent, String startTime,String eventId) {

        this.context = context;
        this.eventId = eventId;
        this.shProducts = shProducts;
        this.status = status;
        this.sourceIntent = sourceIntent;
        this.startTime = startTime;
    }

    public void refreshData(List<ProductMessage> shProducts,String eventId,String startTime,String status) {
        this.eventId = eventId;
        this.shProducts = shProducts;
        this.startTime = startTime;
        this.status = status;
        notifyDataSetChanged();
//        notifyItemChanged(position);
    }

    public void setOnClickStatusListener(OnClickStatusListener clickStatusListener){
        this.clickStatusListener = clickStatusListener;
    }

    @NonNull
    //创建新View，被LayoutManager所调用
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_time_product_item, parent, false);
        RecyclerView.ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {


        ViewHolder viewHolder = (ViewHolder) holder;

        ProductMessage shProduct = shProducts.get(position);

        viewHolder.desc.setText(shProduct.getTitle());
        viewHolder.desc.setMaxLines(2);
        viewHolder.desc.setEllipsize(TextUtils.TruncateAt.END);
        //viewHolder.price.setText("￥" + shProduct.getSh_show_price());
        Utils.set_price_style2(viewHolder.price, "¥ " + shProduct.getPrice());
        //Utils.set_price_style(viewHolder.line_price,"原价 ¥" + shProduct.getSh_line_price());
        String tip = "原价";
//        if(!"2".equals(status)){
//            tip = "原价";
//        }
        Utils.setDin(viewHolder.price, context);
        //Utils.setDin(viewHolder.tvMoneyEarn, context);
        viewHolder.tvMoneyEarn.setText("赚" + shProduct.getCommission());
        viewHolder.sale_progress.setText("已售" + shProduct.getProgress() + "%");
        int progress = (int) Float.valueOf(shProduct.getProgress()).floatValue();
        viewHolder.progressBar.setProgress(progress);
//        App.picasso.load(shProduct.getSh_image())
//                //.resize(ScreenUtils.dp2px(180),ScreenUtils.dp2px(120))
//                .placeholder(R.mipmap.defaults_1)
//                .into(viewHolder.image);
        GlideUtil.displayRoundImage(context, shProduct.getImage(), viewHolder.image, 7, R.mipmap.defaults);

        if (TextUtils.isEmpty(shProduct.getCommission()) ||
                Double.parseDouble(shProduct.getCommission()) == 0) {
            viewHolder.tvMoneyEarn.setVisibility(View.GONE);
        } else {
            viewHolder.tvMoneyEarn.setVisibility(View.VISIBLE);
        }

        viewHolder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, NewCommodityDetailActivity.class);
                intent.putExtra("id", shProduct.getProduct_id());
                intent.putExtra("source_page", sourceIntent);
                intent.putExtra("source_module","限时购");
                intent.putExtra("source_element", startTime);
                intent.putExtra("source_position",position+"");
                context.startActivity(intent);
            }
        });

        if ("4".equals(status) | "5".equals(status)) {
            viewHolder.progressBar.setVisibility(View.GONE);
            viewHolder.sale_progress.setVisibility(View.GONE);
        } else {
            viewHolder.progressBar.setVisibility(View.VISIBLE);
            viewHolder.sale_progress.setVisibility(View.VISIBLE);
        }

        if (Long.valueOf(shProduct.getStock()) > 0) {
            viewHolder.sell_out_layout.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.sell_out_layout.setVisibility(View.VISIBLE);
        }

        viewHolder.buy_btn.setOnClickListener(view -> clickStatusListener.clickStatusBtn(shProduct.getRemind_state()==null?"0":shProduct.getRemind_state(),shProduct.getProduct_id(),eventId,startTime));
        if ("1".equals(shProduct.getRemind_state())) {
            //1: 未开抢（开抢提醒)
            viewHolder.buy_btn.setVisibility(View.VISIBLE);
            viewHolder.buy_btn.setText("开售提醒");
            viewHolder.buy_btn.setBackgroundResource(R.drawable.bg_rect_buy_black);
            viewHolder.buy_btn.setTextColor(Color.parseColor("#FFFFFF"));
        } else if ("2".equals(shProduct.getRemind_state())) {
            //2: 取消提醒
            viewHolder.buy_btn.setVisibility(View.VISIBLE);
            viewHolder.buy_btn.setText("已设置提醒");
            viewHolder.buy_btn.setBackgroundResource(R.drawable.bg_rect_tag_black);
            viewHolder.buy_btn.setTextColor(Color.parseColor("#333333"));
        } else {
            //其他
            if(Long.valueOf(shProduct.getStock()) <= 0 | "4".equals(status) | "5".equals(status)){
                viewHolder.buy_btn.setVisibility(View.INVISIBLE);
            }else{
                viewHolder.buy_btn.setVisibility(View.VISIBLE);
            }

            if("2".equals(status)){
                viewHolder.buy_btn.setText("已售罄");
                viewHolder.buy_btn.setBackgroundResource(R.drawable.new_detail_sellout_bkg);
                viewHolder.buy_btn.setTextColor(Color.parseColor("#FFFFFF"));
            }else{
                viewHolder.buy_btn.setText("立即抢购");
                viewHolder.buy_btn.setBackgroundResource(R.drawable.bg_rect_buy_black);
                viewHolder.buy_btn.setTextColor(Color.parseColor("#FFFFFF"));
            }
        }

        double ratio = 1;
        if (!TextUtils.isEmpty(shProduct.getImage_width())){
            ratio = ScreenUtils.dp2px(107)/Double.parseDouble(shProduct.getImage_width());
        }
        if(shProduct.getIcon_info()!=null
                && !TextUtils.isEmpty(shProduct.getIcon_info().iconHeight)
                &&!TextUtils.isEmpty(shProduct.getIcon_info().iconWidth)) {
            int leftMargin = (int) (Integer.parseInt(shProduct.getIcon_info().iconLeftMargin)*ratio);
            int topMargin = (int) (Integer.parseInt(shProduct.getIcon_info().iconTopMargin)*ratio);
            int bottomMargin = (int) (Integer.parseInt(shProduct.getIcon_info().iconBottomMargin)*ratio);
            ViewTransformUtil.glideImageView(context,shProduct.getIcon_info().iconImage,viewHolder.ivActivity,R.mipmap.defaults);
            viewHolder.ivActivity.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewHolder.ivActivity.getLayoutParams();
            params.height = (int) (Integer.valueOf(shProduct.getIcon_info().iconHeight)*ratio);
            params.width = (int) (Integer.valueOf(shProduct.getIcon_info().iconWidth)*ratio);
            params.addRule(RelativeLayout.ALIGN_BOTTOM,0);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,0);
            params.setMargins(0, 0, 0, 0);
            switch (shProduct.getIcon_info().iconMarginType) {
                case "1":
                    params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.image);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    params.setMargins(leftMargin, topMargin, 0, bottomMargin);
                    break;
                case "2":
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    params.setMargins(leftMargin, topMargin, 0, bottomMargin);
                    break;
            }
            viewHolder.ivActivity.setLayoutParams(params);
        }else {
            viewHolder.ivActivity.setVisibility(View.GONE);
        }

//        if(position == shProducts.size() - 1){
//            viewHolder.divider.setVisibility(View.GONE);
//        }else{
//            viewHolder.divider.setVisibility(View.VISIBLE);
//        }
        //日常价
        if (shProduct.getPrice_tag() != null && !TextUtils.isEmpty(shProduct.getPrice_tag().getBase_price_tag())){
            viewHolder.tvDayTxt.setText(shProduct.getPrice_tag().getBase_price_tag());
            if (!TextUtils.isEmpty(shProduct.getDaily_price())){
                viewHolder.tvDayPrice.setText("¥" + shProduct.getDaily_price());
                viewHolder.tvDayPrice.setVisibility(View.VISIBLE);
            }
            viewHolder.tvDayTxt.setVisibility(View.VISIBLE);
        }else {
            viewHolder.tvDayTxt.setVisibility(View.GONE);
            viewHolder.tvDayPrice.setVisibility(View.GONE);
        }

        //官方价
        if (shProduct.getPrice_tag() != null && !TextUtils.isEmpty(shProduct.getPrice_tag().getMarket_price_tag())){
            viewHolder.tvOfficialTxt.setText(shProduct.getPrice_tag().getMarket_price_tag());
            if (!TextUtils.isEmpty(shProduct.getMarket_price())){
                viewHolder.tvOfficialPrice.setText("¥" +shProduct.getMarket_price());
                viewHolder.tvOfficialPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
                viewHolder.tvOfficialPrice.setVisibility(View.VISIBLE);
            }
            viewHolder.tvOfficialTxt.setVisibility(View.VISIBLE);
        }else {
            viewHolder.tvOfficialTxt.setVisibility(View.GONE);
            viewHolder.tvOfficialPrice.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return shProducts.size();
    }

    public interface OnClickStatusListener{
        void clickStatusBtn(String type,String productId,String mapId,String startTime);
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item)
        public View item;
        @BindView(R.id.divider)
        public View divider;
        @BindView(R.id.sell_out_layout)
        public View sell_out_layout;
        @BindView(R.id.desc)
        public TextView desc;
        @BindView(R.id.name)
        public TextView name;
        @BindView(R.id.buy_btn)
        public TextView buy_btn;
        @BindView(R.id.price)
        public TextView price;
        @BindView(R.id.sale_progress)
        public TextView sale_progress;
        @BindView(R.id.image)
        public ImageView image;
        @BindView(R.id.progressBar)
        public ProgressBar progressBar;
        @BindView(R.id.tv_money_earn)
        public TextView tvMoneyEarn;
        @BindView(R.id.iv_activity)
        public ImageView ivActivity;
        @BindView(R.id.tv_day_txt)
        public TextView tvDayTxt;
        @BindView(R.id.tv_day_price)
        public TextView tvDayPrice;
        @BindView(R.id.tv_official_txt)
        public TextView tvOfficialTxt;
        @BindView(R.id.tv_official_price)
        public TextView tvOfficialPrice;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}

